<?php
/**
 * *********************************************************************************************************************
 * 申通派件通知接口（对接深圳 闵思达 MSD）
 * *********************************************************************************************************************
 */

namespace soa\parter\sto;
use artisan\http;

class delivery
{
    /**
     * 服务地址
     * @var string
     */
    protected $service_url = 'http://222.66.39.203:22221/stoPlatform/app/courierSentList!getCourierSentList.action';

    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_msd';

    /**
     * 传输校验的key值
     * @var string
     */
    protected $key = '0bdbb2c325525e986d55ef50ca66d056';



    /**
     * 获取服务名称
     * @return string
     */
    public function getServiceName()
    {
        return $this->service_name;
    }

    /**
     * 获取服务地址
     * @return string
     */
    public function getServiceUrl()
    {
        return $this->service_url;
    }

    /**
     * 获取校验key
     * @return string
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * 获取快递员派件列表
     * @param $siteName string 网点名称
     * @param $empNo string 网点编号
     * @param $beginDate string 开始日期
     * @param $endDate string 结束日志
     * @return array|string
     * {"isSuccess":"true","message":"请求数据成功...","billCodes":["220024076268","868028846611","868028846979","888307376551"]}
     *
     * 成功返回派件清单的列表，失败发挥失败的原因
     */
    public function getDeliveryList($siteName, $empNo, $beginDate='', $endDate='')
    {
        if(empty($siteName) || empty($empNo))
            return array();

        $logistics_interface = json_encode(array(
            'siteName' => $siteName,
            'empNo' => $empNo,
            'beginDate' => $beginDate ? date('Y-m-d H:i:s', strtotime($beginDate)) : date('Y-m-d 00:00:00'),
            'endDate' => $endDate ? date('Y-m-d H:i:s', strtotime($endDate)) : date('Y-m-d 23:59:59')
        ));
        $data_digest = base64_encode( md5( $logistics_interface . $this->getKey()));
		$time1 = time();
        $data=['logistics_interface' => $logistics_interface, 'data_digest' => $data_digest];
        $result = http::get($this->getServiceUrl(), $data, array('TIMEOUT' => 10));
	//logDebug($result, 'delivery', '1');
        //logDebug('调试:申通派件url:'.$this->getServiceUrl().' 参数:'.json_encode($data,JSON_UNESCAPED_UNICODE),'sto_delivery_list_debug',$empNo.'-'.$siteName.'-'.(time()-$time1));
        //logDebug($result,'sto_delivery_list_debug',$empNo.'-'.$siteName.'-'.(time()-$time1));
        if(empty($result)){
        	logError('超时无响应', 'delivery', (time()-$time1).' sto '.$empNo.' '.$siteName);        	 
            return '超时无响应';
        }
        else {
            $result = json_decode($result, true);
            if(json_last_error() ==0 && $result['isSuccess'] == 'true'){
            	$result = isset($result['billCodes'])?$result['billCodes']:(isset($result['courierSentList'])?$result['courierSentList']:[]);
            	logDebug(count($result), 'delivery', (time()-$time1).' sto '.$empNo.' '.$siteName);            	 
            	return $result;
            }
            else{
            	logError($result, 'delivery', (time()-$time1).' sto '.$empNo.' '.$siteName);            	 
            	if($result['isSuccess'] == 'false')            
                	return $result['message'];
            }
        }
    }
}
